<template>
  <div class="pagination">
    <button class="turnPage" @click="turnPage(pageNo-1)" v-show="pageNo > 1">上一页</button>
    <button v-show="showStartEndPage('start')" @click="turnPage(1)">1</button>
    <button v-show="showStartEndPage('omit1')">...</button>
    <button
      v-for="(page, index) in startEnd.end"
      :key="index"
      v-show="showCenterPage(page)"
      :class="{ curPage: page == pageNo }"
      @click="turnPage(page)"
    >
      {{ page }}
    </button>
    <button v-show="showStartEndPage('omit2')">...</button>
    <button v-show="showStartEndPage('end')" @click="turnPage(totalPage)">
      {{ totalPage }}
    </button>
    <button class="turnPage" @click="turnPage(pageNo+1)" v-show="pageNo < totalPage">下一页</button>
    <button style="margin-left: 30px">共{{ total }}条</button>
  </div>
</template>

<script>
export default {
  name: "Pagination",
  props: ["pageNo", "pageSize", "total", "continues"],
  data() {
    return {
      isCur: false,
    };
  },
  computed: {
    totalPage() {
      return Math.ceil(this.total / this.pageSize);
    },
    startEnd() {
      const { continues, pageNo, total } = this;
      let start = 0,
        end = 0;
      //临界条件
      //连续页面超过总页数
      if (continues > total) {
        start = 1;
        end = total;
      } else {
        start = pageNo - parseInt(continues / 2);
        end = pageNo + parseInt(continues / 2);
        //起始位置边界
        if (start < 1) {
          start = 1;
          end = continues;
        }
        //结束位置边界
        if (end > total) {
          start = total - continues + 1;
          end = total;
        }
      }
      return { start, end };
    },
  },
  methods: {
    showCenterPage(page) {
      if (page >= this.startEnd.start && page <= this.totalPage) return true;
      else return false;
    },
    showStartEndPage(type) {
      if (type == "start" && this.startEnd.start > 1) {
        return true;
      } else if (type == "end" && this.startEnd.end < this.totalPage) {
        return true;
      } else if (type == "omit1" && this.startEnd.start > 2) {
        return true;
      } else if (type == "omit2" && this.startEnd.end < this.totalPage - 1) {
        return true;
      } else {
        return false;
      }
    },
    turnPage(page) {
      this.$emit("turnToPage", page);
    },
  },
};
</script>

<style lang="less" scoped>
.pagination {
  text-align: center;
  .curPage {
    background: red;
    color: white;
  }
  button {
    margin: 0 5px;
    background-color: #f4f4f5;
    color: #606266;
    outline: none;
    border-radius: 2px;
    padding: 0 4px;
    vertical-align: top;
    display: inline-block;
    font-size: 13px;
    min-width: 35.5px;
    height: 28px;
    line-height: 28px;
    cursor: pointer;
    box-sizing: border-box;
    text-align: center;
    border: 0;

    &[disabled] {
      color: #c0c4cc;
      cursor: not-allowed;
    }
    &:hover {
      background: lightcoral;
      color: white;
    }
    &:active {
      background: red;
      color: white;
    }
    &.active {
      cursor: not-allowed;
      background-color: #409eff;
      color: #fff;
    }
  }
}
</style>